package com.fzhucc.modules.lesson.rest;

import com.fzhucc.annotation.Log;
import com.fzhucc.modules.lesson.domain.LessonFirstChapter;
import com.fzhucc.modules.lesson.domain.dto.LessonFirstChapterQueryCriteria;
import com.fzhucc.modules.lesson.service.LessonFirstChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author yyg
* @date 2022-12-07
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "课程:文件一级分类管理")
@RequestMapping("/api/lessonFirstChapter")
public class LessonFirstChapterController {

    private final LessonFirstChapterService lessonFirstChapterService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('lessonFirstChapter:list')")
    public void exportLessonFirstChapter(HttpServletResponse response, LessonFirstChapterQueryCriteria criteria) throws IOException {
        lessonFirstChapterService.download(lessonFirstChapterService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询lessonFirstChapter")
    @ApiOperation("查询lessonFirstChapter")
    @PreAuthorize("@el.check('lessonFirstChapter:list')")
    public ResponseEntity<Object> queryLessonFirstChapter(LessonFirstChapterQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(lessonFirstChapterService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("课程赋予文件一级分类")
    @ApiOperation("课程赋予文件一级分类")
    @PreAuthorize("@el.check('lessonFirstChapter:add')")
    public ResponseEntity<Object> createLessonFirstChapter(@Validated @RequestBody LessonFirstChapter resources){
        return new ResponseEntity<>(lessonFirstChapterService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改lessonFirstChapter")
    @ApiOperation("修改lessonFirstChapter")
    @PreAuthorize("@el.check('lessonFirstChapter:edit')")
    public ResponseEntity<Object> updateLessonFirstChapter(@Validated @RequestBody LessonFirstChapter resources){
        lessonFirstChapterService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除lessonFirstChapter")
    @ApiOperation("删除lessonFirstChapter")
    @PreAuthorize("@el.check('lessonFirstChapter:del')")
    public ResponseEntity<Object> deleteLessonFirstChapter(@RequestBody Integer[] ids) {
        lessonFirstChapterService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}